package Part1_java.util.Collection.List;

import org.junit.Test;

import java.util.*;

/**
 * ClassName: ArrayListTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.03.31 22:54
 */
public class ListTest {

    /**
     * Collection常用方法：
     * add(object obj)
     * addAll(Collection coll)加入集合coll所有元素
     * clear()清空
     * isEmpty()判空
     * size()元素个数
     * contains(Object obj)判断是否包含
     * containsAll(Collection coll)判断是否全部包含
     * retainAll(Collection coll)
     * remove(Object obj)
     * removeAll(Collection coll)
     * hashCode()
     * equals()
     * toArray()
     */

    @Test
    public void test1(){
        Collection coll = new ArrayList();

        coll.add("AA");
        coll.add(123);
        coll.add(new User("Tom"));//ArrayList.User@2a18f23c (重写toString方法)---> User{age=0, name='Tom'}
        coll.add(new String("嗡嗡嗡"));

        System.out.println(coll);


        //集合 ---> 数组
        Object[] arr = coll.toArray();
        System.out.println(Arrays.toString(arr));

        //获取哈希值
        System.out.println(coll.hashCode());

    }

    @Test
    public void test2(){
        String[] arr = new String[]{"aa","bb","cc"};

        //数组 ---> 集合
        List list = Arrays.asList(arr);
        System.out.println(list);

        Integer[] arr1 = new Integer[]{1,2,3};//已经装箱
        int[] arr2 = new int[]{1,2,3};//没有装箱

        List list1 = Arrays.asList(arr1);
        List list2 = Arrays.asList(arr2);

        System.out.println(list1);//[1, 2, 3]
        System.out.println(list2);//[I@256216b3]

        System.out.println(list1.size());//3
        System.out.println(list2.size());//1
    }

    @Test
    public void test3(){
        Collection coll = new ArrayList();

        coll.add("AA");
        coll.add(123);
        coll.add(new User("Tom"));//ArrayList.User@2a18f23c (重写toString方法)---> User{age=0, name='Tom'}
        coll.add(new String("嗡嗡嗡"));

        //创建迭代器的实现类对象
        Iterator iterator = coll.iterator();

        /*一次输出一个
        System.out.println(iterator.next());
        System.out.println(iterator.next());
        System.out.println(iterator.next());*/

        //推荐写法
        while (iterator.hasNext()){//判断是否存在下一个对象元素
            System.out.println(iterator.next());
            //next()：1、指针下移 2、将下移后位置上元素返回
        }
    }

    @Test
    public void test4(){
        Collection coll = new ArrayList();

        coll.add("AA");
        coll.add(123);
        coll.add(new User("Tom"));//ArrayList.User@2a18f23c (重写toString方法)---> User{age=0, name='Tom'}
        coll.add(new String("嗡嗡嗡"));


        //JDK5之后的增强for循环：对于集合，底层使用迭代器来实现
        for(Object obj : coll){
            System.out.println(obj);
        }
    }

    @Test
    public void test5(){
        List list = new ArrayList();

        list.add("!");
        list.add(123);
        list.add(2);
        list.add(new User("Four",12));

        System.out.println(list);

        //插入!!
        list.add(2,"!!");
        System.out.println(list);

        List list1 = Arrays.asList(1,2,3);

        //addAll():插入list1的所有元素
        //add():list1整体作为一个元素插入
        list.addAll(1, list1);
        //list.add(1, list1);
        System.out.println(list);


        //删除索引2
        list.remove(2);
        System.out.println(list);

        //删除数据2
        list.remove(Integer.valueOf(2));//2装箱为值为2的对象
        System.out.println(list);
    }

    @Test
    public void test6(){
        List list = new ArrayList();

        list.add(0,"!");
        list.add(1,1);
        list.add(2,88);
        list.add(3, new User("Four",12));

        //遍历：增强for循环
        for(Object obj : list){
            System.out.println(obj);
        }

        //遍历：普通for循环
        for(int i=0 ; i<list.size() ; i++){
            System.out.println(list.get(i));
        }
    }
}
